package com.cooper.slidingmenu.utils;

import com.cooper.scrapemedialib.R;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class SampleInputDialog extends DialogFragment implements
		android.view.View.OnClickListener {

	public interface SampleInputDialogListener {

		public void onSampleInputDialogReturn(int which, boolean isOk,
				String text);
	}

	public static SampleInputDialog newInstance(int which, int titleRes,
			String initText) {
		SampleInputDialog result = new SampleInputDialog();
		Bundle bundle = new Bundle();
		bundle.putInt("WHICH", which);
		bundle.putInt("TITLE_RES", titleRes);
		bundle.putString("INPUT", initText);
		result.setArguments(bundle);
		return result;
	}

	public static SampleInputDialog newInstance(int which, String title,
			String initText) {
		SampleInputDialog result = new SampleInputDialog();
		Bundle bundle = new Bundle();
		bundle.putInt("WHICH", which);
		bundle.putString("TITLE", title);
		bundle.putString("INPUT", initText);
		result.setArguments(bundle);
		return result;
	}

	private EditText inputEdit = null;

	@Override
	public void onSaveInstanceState(Bundle savedInstanceState) {
		super.onSaveInstanceState(savedInstanceState);
		if (inputEdit != null)
			getArguments().putString("INPUT", inputEdit.getText().toString());
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (android.os.Build.VERSION.SDK_INT < 11) {
			setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Dialog);
		} else {
			setStyle(DialogFragment.STYLE_NORMAL,
					android.R.style.Theme_DeviceDefault_Dialog);
		}
		setCancelable(true);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View result = inflater.inflate(R.layout.dialog_edit_input, container,
				false);
		inputEdit = ((EditText) result.findViewById(R.id.dialog_edit));
		inputEdit.setText(getArguments().getString("INPUT"));

		if (getArguments().containsKey("TITLE_RES")) {
			getDialog().setTitle(getArguments().getInt("TITLE_RES"));
		}else
			getDialog().setTitle(getArguments().getString("TITLE"));

		((Button) result.findViewById(R.id.btn_save)).setOnClickListener(this);
		((Button) result.findViewById(R.id.btn_cancel))
				.setOnClickListener(this);
		return result;
	}

	@Override
	public void onCancel(DialogInterface dialog) {
		super.onCancel(dialog);
		((SampleInputDialogListener) getActivity()).onSampleInputDialogReturn(
				getArguments().getInt("WHICH"), false, inputEdit.getText()
						.toString());

	}

	/*
	 * 
	 * @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
	 * return new AlertDialog.Builder(getActivity())
	 * .setIcon(R.drawable.ic_dialog_icon)
	 * .setTitle(R.string.search_online_movie) .setView(
	 * LayoutInflater.from(PolymericMoviesActivity.this)
	 * .inflate(R.layout.dialog_edit_input, null))
	 * .setPositiveButton(R.string.alert_dialog_ok, new
	 * DialogInterface.OnClickListener() {
	 * 
	 * @Override public void onClick(DialogInterface dialog, int which) { if
	 * (startForSearch || (fanartAdapter == null)) { fanartAdapter = new
	 * PolymericMovieFanartAdapter( PolymericMoviesActivity.this,
	 * R.xml.flvcd_movie); fanartAdapter
	 * .setDataChangedListener(PolymericMoviesActivity.this); } else {
	 * fanartAdapter .switchScraper(R.xml.flvcd_movie); }
	 * 
	 * fanartAdapter .setMediaCollector(new
	 * SearchResourceCollector<OnlineMovie>( ((EditText) ((Dialog) dialog)
	 * .findViewById(R.id.dialog_edit)) .getText() .toString().trim())); if
	 * (startForSearch) { switchMediaViewShow(); } else {
	 * fanartAdapter.refresh(); }
	 * 
	 * startForSearch = false; } })
	 * .setNeutralButton(R.string.alert_dialog_cancel, new
	 * DialogInterface.OnClickListener() {
	 * 
	 * @Override public void onClick(DialogInterface dialog, int which) { if
	 * (startForSearch) { finish(); } } }).setOnCancelListener(new
	 * OnCancelListener() {
	 * 
	 * @Override public void onCancel(DialogInterface dialog) { if
	 * (startForSearch) { finish(); } } }).create(); }
	 */

	@Override
	public void onClick(View v) {
		dismiss();
		((SampleInputDialogListener) getActivity()).onSampleInputDialogReturn(
				getArguments().getInt("WHICH"), v.getId() == R.id.btn_save,
				inputEdit.getText().toString());

	}
}
